var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },
  pageSize:100,
  pageIndex:1,
  secondNoTapId:'',
  goodsCarNumber:0,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var catIds = options.parentId
    var that = this;
    const firstCatId = options.firstCatId
    const secondCatIdClass = options.secondCatId
    if (catIds == undefined){
      wx.request({
        url: app.globalData.url+'getSecondcCategoriesByParentId',
        data: {
          parentId: firstCatId
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'GET',
        success: function (res) {
          that.setData({
            secondCatName: res.data.object
          })
          //首次加载页面，二级分类第一个id（查询二级分类的全部goods列表）
          const secondCatId = secondCatIdClass
          //点击“三级”，不点击“二级”，直接拿二级分类
          that.secondNoTapId = secondCatId
         
          // TODO全部 -- 请求接口
          wx.request({
            url: app.globalData.url + 'getGoodsByNonLeafCatId',
            data: {
              catId: secondCatId,
              pageIndex: that.pageIndex,
              pageSize: that.pageSize 
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            method: 'GET',
            success: function (res) {
              if (res.data.resultMsg == "没有查到该分类的商品信息"){
                that.setData({
                  goods: '',
                  show: false,
                })
              }else{
                that.setData({
                  goods: res.data.object.goods,
                  show: true,
                })
              }
            },
            fail: function (res) { },
            complete: function (res) { },
          })  
          if (res.data.resultMsg == "没有查到二级分类信息") {
            console.log(res.data.resultMsg)
          } else if (res.data.object == undefined) {
            const secondCatId = options.secondCatId
            console.log(secondCatId)
            /**
             * 点击二级查----》三级分类
             */
            wx.request({
              url: app.globalData.url+'getThirdCategoriesByParentId',
              data: {
                parentId: secondCatId,
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              method: 'GET',
              success: function (res) {
                that.setData({
                  thirdCatNames: res.data.object,
                  active: '-1'
                })
                if (res.data.resultMsg == "没有查到三级分类信息") {
                } else {
                  const thirdCatId = res.data.object[0].thirdCatId;
                  wx.request({
                    url: app.globalData.url+'getGoodsByCatId',
                    data: {
                      catId: thirdCatId,
                      pageSize: that.pageSize,
                      pageIndex: that.pageIndex,
                    },
                    header: {
                      'content-type': 'application/json' // 默认值
                    },
                    method: 'GET',
                    success: function (res) {
                      if (res.data.resultMsg == "没有查到商品的相关信息") {
                        that.setData({
                          show: false,
                          goods: '',
                        })
                      } else {
                        that.setData({
                          show: true,
                          goods: res.data.object.goods,
                        })
                      }
                    },
                    fail: function (res) { },
                    complete: function (res) { },
                  })
                }
              },
              fail: function (res) { },
              complete: function (res) { },
            })

          } else {
            // const secondCatId = res.data.object[0].secondCatId;
            const secondCatId = options.secondCatId
            console.log(secondCatId)
            /**
             * 二级查----》三级分类
             */
            wx.request({
              url: app.globalData.url+'getThirdCategoriesByParentId',
              data: {
                parentId: secondCatId,
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              method: 'GET',
              success: function (res) {
                that.setData({
                  thirdCatNames: res.data.object,
                  active: '-1',
                  _num: secondCatId
                })
                if (res.data.resultMsg == "没有查到三级分类信息") {
                } else {
                  const thirdCatId = res.data.object[0].thirdCatId;
                  wx.request({
                    url: app.globalData.url+'getGoodsByCatId',
                    data: {
                      catId: thirdCatId,
                      pageSize: that.pageSize,
                      pageIndex: that.pageIndex,
                    },
                    header: {
                      'content-type': 'application/json' // 默认值
                    },
                    method: 'GET',
                    success: function (res) {
                      if (res.data.resultMsg == "没有查到商品的相关信息") {
                        that.setData({
                          show: false,
                          goods: '',
                        })
                      } else {
                        that.setData({
                          show: true,
                          goods: res.data.object.goods,
                        })
                      }
                    },
                    fail: function (res) { },
                    complete: function (res) { },
                  })
                }
              },
              fail: function (res) { },
              complete: function (res) { },
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      });
    }else{
      wx.request({
        url: app.globalData.url+'getSecondcCategoriesByParentId',
        data: {
          parentId: catIds
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'GET',
        success: function (res) {
          that.setData({
            _num: res.data.object[0].secondCatId,
            secondCatName: res.data.object
          })
          //首次加载页面，二级分类第一个id（查询二级分类的全部goods列表）
          const secondCatId = res.data.object[0].secondCatId 
          
          //点击“三级”，不点击“二级”，直接拿二级分类
          that.secondNoTapId = secondCatId

          // TODO全部 -- 请求接口
          wx.request({
            url: app.globalData.url + 'getGoodsByNonLeafCatId',
            data: {
              catId: secondCatId ,
              pageIndex: that.pageIndex,
              pageSize: that.pageSize
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            method: 'GET',
            success: function (res) {
              if (res.data.resultMsg == "没有查到该分类的商品信息") {
                that.setData({
                  goods: '',
                  show: false,
                })
              } else {
                that.setData({
                  goods: res.data.object.goods,
                  show: true,
                })
              }
            },
            fail: function (res) { },
            complete: function (res) { },
          })  
          if (res.data.resultMsg == "没有查到二级分类信息") {
          } else if (res.data.object == undefined) {
            const secondCatId = options.secondCatId
            /**
             * 点击二级查----》三级分类
             */
            wx.request({
              url: app.globalData.url+'getThirdCategoriesByParentId',
              data: {
                parentId: secondCatId,
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              method: 'GET',
              success: function (res) {
                that.setData({
                  thirdCatNames: res.data.object,
                  active: '-1',
                })
                if (res.data.resultMsg == "没有查到三级分类信息") {
                } else {
                  const thirdCatId = res.data.object[0].thirdCatId;
                  wx.request({
                    url: app.globalData.url+'getGoodsByCatId',
                    data: {
                      catId: thirdCatId,
                      pageSize: that.pageSize,
                      pageIndex: that.pageIndex,
                    },
                    header: {
                      'content-type': 'application/json' // 默认值
                    },
                    method: 'GET',
                    success: function (res) {
                      if (res.data.resultMsg == "没有查到商品的相关信息") {
                        that.setData({
                          show: false,
                          goods: '',
                        })
                      } else {
                        that.setData({
                          show: true,
                          goods: res.data.object.goods,
                        })
                      }
                    },
                    fail: function (res) { },
                    complete: function (res) { },
                  })
                }
              },
              fail: function (res) { },
              complete: function (res) { },
            })

          } else {
            const secondCatId = res.data.object[0].secondCatId;
            // const secondCatId = options.secondCatId
            /**
             * 点击二级查----》三级分类
             */
            wx.request({
              url: app.globalData.url+'getThirdCategoriesByParentId',
              data: {
                parentId: secondCatId,
                // parentId:2
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              method: 'GET',
              success: function (res) {
                that.setData({
                  thirdCatNames: res.data.object,
                  active: '-1'
                })
                if (res.data.resultMsg == "没有查到三级分类信息") {
                } else {
                  const thirdCatId = res.data.object[0].thirdCatId;
                  wx.request({
                    url: app.globalData.url+'/getGoodsByCatId',
                    data: {
                      catId: thirdCatId,
                      pageSize: that.pageSize,
                      pageIndex: that.pageIndex,
                    },
                    header: {
                      'content-type': 'application/json' // 默认值
                    },
                    method: 'GET',
                    success: function (res) {
                      if (res.data.resultMsg == "没有查到商品的相关信息") {
                        that.setData({
                          show: false,
                          goods: '',
                        })
                      } else {
                        that.setData({
                          show: true,
                          goods: res.data.object.goods,
                        })
                      }
                    },
                    fail: function (res) { },
                    complete: function (res) { },
                  })
                }
              },
              fail: function (res) { },
              complete: function (res) { },
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      });
    }

    /**查询购物车列表 */
    that.queryCarList();
  },
  onShow:function(){
    /**查询购物车列表 */
    this.queryCarList();    
  },
  /**
   * 三级分类--筛选
   * 
   */
  thirdItem: function (e) {
    var that = this;
    const index = e.currentTarget.dataset.index;
    if (index == -1) {
      // TODO全部 -- 请求接口
      wx.request({
        url: app.globalData.url + 'getGoodsByNonLeafCatId',
        data: {
          catId: that.secondNoTapId,
          pageIndex: that.pageIndex,
          pageSize:that.pageSize
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'GET',
        success: function(res) {
          if (res.data.resultMsg == "没有查到该分类的商品信息") {
            that.setData({
              goods: '',
              show: false,
            })
          } else {
            that.setData({
              goods: res.data.object.goods,
              show: true,
            })
          }
          that.setData({
            active: -1
          })
        },
        fail: function(res) {},
        complete: function(res) {},
      })
    } else {
      that.setData({
        active: e.currentTarget.dataset.index
      })
      const thirdCatId = e.currentTarget.dataset.thirdcatid
      wx.request({
        url: app.globalData.url+'getGoodsByCatId',
        data: {
          catId: thirdCatId,
          pageSize: that.pageSize,
          pageIndex:that.pageIndex
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'GET',
        success: function (res) {
          if (res.data.resultMsg == "没有查到商品的相关信息") {
            that.setData({
              show: false,
              goods: ''
            })
          } else {
            that.setData({
              show: true,
              goods: res.data.object.goods
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },
  detailCommodity: function (e) {
    const goodsId = e.currentTarget.dataset.goodsid
    wx.navigateTo({
      url: '../commodityDetail/commodityDetail?goodsId=' + goodsId,
    })
  },
  shoppingCar: function () {
    wx.navigateTo({
      url: '../shoppingCar/shoppingCar',
    })
  },
  /**
   * 二级分类
   */
  commodityClassifyItem: function (e) {
    console.log(e.currentTarget.dataset.secondcatid)
    var that = this;
    this.setData({
      _num: e.currentTarget.dataset.secondcatid,
      active: -1
    })
    const catId = e.currentTarget.dataset.secondcatid
    /**
     * 点击二级查----》三级分类、商品列表（全部的商品列表）
     */
    wx.request({
      url: app.globalData.url+'getThirdCategoriesByParentId',
      data: {
        parentId: catId,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        that.setData({
          thirdCatNames: res.data.object,
        })
        if (res.data.resultMsg == "没有查到三级分类信息") {
        } else {
          that.thirdCatId = res.data.object[0].thirdCatId;
        }
        // TODO 全部数据
        wx.request({
          url: app.globalData.url + 'getGoodsByNonLeafCatId',
          data: {
            catId: catId,
            pageIndex: that.pageIndex,
            pageSize: that.pageSize
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          method: 'GET',
          success: function (res) {
            if (res.data.resultMsg == "没有查到该分类的商品信息") {
              that.setData({
                goods: '',
                show: false,
              })
            } else {
              that.setData({
                goods: res.data.object.goods,
                show: true,
              })
            }
          },
          fail: function (res) { },
          complete: function (res) { },
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  addShopping: function (e) {
    const goodsName = e.currentTarget.dataset.goodsname
    const goodsPrice = e.currentTarget.dataset.goodsprice
    const goodsIds = e.currentTarget.dataset.goodsid
    const originalImg = e.currentTarget.dataset.originalimg 
    const userId = app.globalData.userid
    
    wx.request({
      url: app.globalData.url + 'insertGoodsInCartShop',
      data: {
        goodsNumber: 1,
        goodsPic: originalImg,
        goodsName: goodsName,
        goodsPrice: goodsPrice,
        goodsId: goodsIds,
        cartType: 2,
        userId: userId,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 2000,
          mask: true,
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**首页购物车跳转 */
  InshoppingCar: function () {
    wx.navigateTo({
      url: '../shoppingCar/shoppingCar',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**查询购物车列表 */
  queryCarList: function () {
    const that = this;
    const userId = app.globalData.userid
    wx.request({
      url: app.globalData.url + 'getShopCart',
      data: {
        userId: userId,
        cartType: 2,
        pageIndex: 1,
        pageSize: 100
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        that.goodsCarNumber = 0
        if (res.data.object.cartList != undefined) {

          for (let i = 0; i < res.data.object.cartList.length; i++) {
            that.goodsCarNumber += res.data.object.cartList[i].goodsNumber
          }
          that.setData({
            goodsCarNumber: that.goodsCarNumber
          })
        } else {
          that.setData({
            goodsCarNumber: 0
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  }
})